package com.didi.chameleon.example;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.didi.chameleon.weex.container.CmlWeexView;

import java.net.URLEncoder;

public class CmlWeexViewTestActivity extends AppCompatActivity implements View.OnClickListener {

    private Button index;
    private Button page1;
    private Button page2;
    private CmlWeexView cmlView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cml_weex_test_view);

        index = findViewById(R.id.index);
        page1 = findViewById(R.id.page1);
        page2 = findViewById(R.id.page2);
        cmlView = findViewById(R.id.cml_view);

        index.setOnClickListener(this);
        page1.setOnClickListener(this);
        page2.setOnClickListener(this);

        cmlView.onCreate();
        String weexUrl = "http://172.168.1.26:8000/weex/cml-demo.js";
        String path = "/pages/index/index";
        String url = String.format("http://172.168.1.26:8000/cml/h5/index?wx_addr=%s&path=%s",
                URLEncoder.encode(weexUrl), URLEncoder.encode(path));
        cmlView.render(url, null);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.index: {
                String weexUrl = "http://172.168.1.26:8000/weex/cml-demo.js";
                String path = "/pages/index/index";
                String url = String.format("http://172.168.1.26:8000/cml/h5/index?wx_addr=%s&path=%s",
                        URLEncoder.encode(weexUrl), URLEncoder.encode(path));
                cmlView.getInstance().reload(url);
                break;
            }
            case R.id.page1: {
                String weexUrl = "http://172.168.1.26:8000/weex/cml-demo.js";
                String path = "/pages/test1/test1";
                String url = String.format("http://172.168.1.26:8000/cml/h5/index?wx_addr=%s&path=%s",
                        URLEncoder.encode(weexUrl), URLEncoder.encode(path));
                cmlView.getInstance().reload(url);
                break;
            }
            case R.id.page2: {
                String weexUrl = "http://172.168.1.26:8000/weex/cml-demo.js";
                String path = "/pages/test2/test2";
                String url = String.format("http://172.168.1.26:8000/cml/h5/index?wx_addr=%s&path=%s",
                        URLEncoder.encode(weexUrl), URLEncoder.encode(path));
                cmlView.getInstance().reload(url);
                break;
            }
        }
    }
}
